package dept_api

import (
	"benkendSystem/database"
	"benkendSystem/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

// GetUserFromDepart 根据部门查用户
func GetUserFromDepart(c *gin.Context) {
	in := new(GetUserFromDepartRequest)
	err := c.ShouldBindJSON(in)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
		})
		return
	}
	var departId int64
	err = database.DB.Model(new(models.Department)).Select("id").Where("name = ?", in.Name).Find(&departId).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "系统错误",
		})
		return
	}
	var data []string
	err = database.DB.Model(new(models.User)).Select("username").Joins("left join user_depart ud on user.id=ud.user_id").Where("depart_id=?", departId).Find(&data).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "操作失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":   200,
		"msg":    "操作成功",
		"result": data,
	})
}
